package com.fancon.android.gardenjokesx.dialog;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.facebook.android.AsyncFacebookRunner;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.activity.S03_1Activity;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.rpc.GardenJokesApiConnect;
import com.fancon.android.gardenjokesx.social.BaseRequestListener;
import com.fancon.android.gardenjokesx.social.DatabaseHelper;
import com.fancon.android.gardenjokesx.social.SessionEvents;
import com.fancon.android.gardenjokesx.social.SocialUntil;
import com.fancon.android.gardenjokesx.social.Utility;
import com.fancon.android.gardenjokesx.utils.UserUtil;

public class S03_6Dialog extends Dialog {
	private TextView mTvMessage;
	private DatabaseHelper database;
	private Activity activity;
	private ImageView arrows;
	
	public S03_6Dialog(Activity context, final RadioButton radioButton, DatabaseHelper db, ImageView arr) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.s03_6);
		setCancelable(true);
		//getWindow().setGravity(Gravity.BOTTOM);
		
		activity = context;
		mTvMessage = (TextView)findViewById(R.id.s03_6_tvLinkMessage);
		arrows = arr;
		
		database = db;
		
		Button btnCancel = (Button) findViewById(R.id.s03_6_btnCancel);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		
		Button btnUnlink = (Button) findViewById(R.id.s03_6_btnLink);
		btnUnlink.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				User user = new User();
				user = UserUtil.getInfoUserLogin(activity);
				
		        switch (radioButton.getId()) {
		        case R.id.s03_radiobtnTwitter:	
		        	String twitterToken = database.getTwitterUser(user.getUser_id()).getTwitterToken();
		        	String twitterSecret = database.getTwitterUser(user.getUser_id()).getTwitterSecret();
		        	SocialUntil s = new SocialUntil(activity);
		        	int twitterID = s.getTwitterID(twitterToken, twitterSecret);
		        	registerLink(user.getUser_id(), Integer.toString(twitterID), "T", twitterToken, twitterSecret, true);
		        	
					database.deleteTwitterUser(user.getUser_id());
					if (S03_1Activity.mSession != null && S03_1Activity.mSession.getAccessToken() != null) {
						S03_1Activity.mAccessToken = null;
						S03_1Activity.mSession.resetAccessToken();
						S03_1Activity.mTwitter = null;
					}
		        	break;
		        case R.id.s03_radiobtnFacebook:	
		        	String facebookToken = database.getFacebookUser(user.getUser_id()).getAccessToken();
		        	String facebookID = database.getFacebookUser(user.getUser_id()).getFacebookId();
		        	registerLink(user.getUser_id(), facebookID, "F", facebookToken, "", true);
		        	
		        	database.deleteFacebookUser(user.getUser_id());
		    		if (Utility.mFacebook != null && Utility.mFacebook.getAccessToken() != null) {
		    			Log.w("LOG OUT FB","LOUT FB");
						SessionEvents.onLogoutBegin();
						AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(Utility.mFacebook);
						asyncRunner.logout(activity, new LogoutRequestListener());
		    		}
		        	break;
		        case R.id.s03_radiobtnFlickr:	
		        	database.deleteFlickrUser(user.getUser_id());
		        	break;
		        case R.id.s03_radiobtnWeibo:	
		        	database.deleteWeiboUser(user.getUser_id());
		        	break;
		        case R.id.s03_radiobtnTumblr:	
		        	database.deleteTumblrUser(user.getUser_id());
		        	break;
		        default:
		        	break;
		        }

		        radioButton.setButtonDrawable(R.drawable.s03_radio_button_not_sellected);
		        arrows.setVisibility(View.VISIBLE);
				dismiss();
			}
		});
	}

	public void setMessage(String service){
		String msg = getContext().getResources().getString(R.string.S03_6_MSG_S0006, service);
//		msg = msg.replace("{sevice name}", service);
		mTvMessage.setText(msg);
	}
	
    private class LogoutRequestListener extends BaseRequestListener {
        @Override
        public void onComplete(String response, final Object state) {
        	SessionEvents.onLogoutFinish();
        }
    }

    
	public void registerLink(final String user_id, final String id, final String type, final String token, final String secret, final Boolean is_cancel) {
        Thread thread = new Thread() {
            public void run() {
        		List<NameValuePair> request = new ArrayList<NameValuePair>(6);
        		request.add(new BasicNameValuePair("user_id", user_id));
        		request.add(new BasicNameValuePair("id", id));
        		request.add(new BasicNameValuePair("type", type));
        		request.add(new BasicNameValuePair("token", token));
        		request.add(new BasicNameValuePair("secret", secret));
        		if (is_cancel == false) {
        			request.add(new BasicNameValuePair("is_cancel", "0"));
        		} else {
        			request.add(new BasicNameValuePair("is_cancel", "1"));
        		}
        		GardenJokesApiConnect api = new GardenJokesApiConnect(activity);
        		String req = api.execPost(activity, GardenJokesApiConnect.GROUP_S, "registerLinkToOtherServices", request);
        		if (req != null && req.length() >0) {
        			Log.d("S03_6 registerLinkToOtherServices", req);
        		}
            } // end run
        };	// end Thread
        thread.start();

	}
    
}
